/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

import java.sql.SQLException;

public class FieldAutoId extends FieldLong implements FieldId {

	public FieldAutoId( String p_name, int p_field_type ,String p_description,Entity entity ) {
		super( p_name,p_field_type | Field.FIELD_KEY,p_description,entity );
		entity.setFieldId(this);
	}
	
	public Field newInstance(Entity entity) {
		return new FieldAutoId(getName(),getFieldType(),getDescription(),entity);
	}

	/**
	 * Some databases uses generator sql
	 */
	public void set(DBConnection connection) 
	throws SQLException {
		connection.getDataBaseUtils().setBeforeInsert(connection,this);
	}

	/**
	 * Loads the id field as soon as an insertion has been made
	 */
	public void loadAfterInsert(DBConnection connection)
	throws SQLException {
		connection.getDataBaseUtils().loadIdAfterInsert(connection,this);
	}

	public void set(Object object)
	throws DataException {
		setObject(object);
	}

	public String getAsStringSQL() {return ""+get();}
	
	public String getAsString() {return ""+get();}


	public void setAsString(String value)
	throws DataException {
		try {
         if (value != null && value.length() > 0) {
            set(new Long(value));
         } 
		} catch (Exception exception) {
			throw new DataException(exception);
		}
	}

	public Long getAsLong() throws DataException {return get();}
	
	public Object getAsObject() throws DataException {return get();}
	
}
